package com.watson.juc.pc;

import java.util.concurrent.TimeUnit;

/**
 * @author watson
 * @date 2021/7/29 17:00
 * @Description: TODO
 **/
public class eightLock {
    public static void main(String[] args) throws InterruptedException {
        Phone phone = new Phone();
        Phone phone2 = new Phone();


        //synchronized 锁的对象this是方法段得调用者
        //两个方法用的同一把锁，谁先拿到先执行
        new Thread(() -> {
            phone.sendSms();
        }, "A").start();

        TimeUnit.SECONDS.sleep(1);

        new Thread(() -> {
            phone2.call();
//            phone.hello();
        }, "B").start();
    }
}

class Phone {
    public synchronized void sendSms() {
        try {
            TimeUnit.SECONDS.sleep(4);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("sendSms");
    }

    public synchronized void call() {
        System.out.println("call");
    }

    //3.一个对象时添加一个普通方法调用，普通方法后发现和同步锁无关
    public void hello() {
        System.out.println("hello");
    }
}
